DEFINE CLASS FileOpenClose AS CUSTOM

  #DEFINE GENERIC_READ           0x80000000
  #DEFINE FILE_SHARE_READ        0x00000001  
  #DEFINE FILE_SHARE_WRITE       0x00000002
  #DEFINE FILE_FLAG_NO_BUFFERING 0x20000000
  #DEFINE OPEN_EXISTING          0x00000003

  * API Open and close exiting file routines
  
  * The file handle
  nFileHandle = 0
  
  PROCEDURE Init
    
    DECLARE SHORT CloseHandle IN Win32API;
      INTEGER hObject
    DECLARE INTEGER CreateFile IN Win32API;
      STRING @lpFileName, INTEGER dwDesiredAccess,;
      INTEGER dwShareMode, STRING @lpSecurityAttributes,;
      INTEGER dwCreationDistribution,;
      INTEGER dwFlagsAndAttributes,;
      INTEGER hTemplateFile
    RETURN
  ENDPROC
  
  FUNCTION OpenFile
  
    LPARAMETERS pcFileName, pnMode
    * pnMode is either GENERIC_READ or
    * GENERIC_WRITE
    
    LOCAL llresult, lcfilename, lnaccess
    lcfilename = pcFileName
    IF This.nFileHandle # 0
      This.FileClose
    ENDIF
    IF pnMode = GENERIC_READ
      lnaccess = FILE_SHARE_READ
    ELSE
      lnaccess = FILE_SHARE_WRITE
    ENDIF
    This.nFileHandle = CreateFile(@lcfilename, pnMode,;
      lnaccess, 0, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, 0)
    llresult = (This.nFileHandle > -1)
    IF NOT llresult
      This.nFileHandle = 0
    ENDIF
    RETURN llresult
  ENDFUNC
  
  PROCEDURE CloseFile
    
    IF This.nFileHandle # 0
      = CloseHandle(This.nFileHandle)
      This.nFileHandle = 0
    ENDIF
    RETURN
  ENDPROC
  
  PROCEDURE Destroy
  
    This.CloseFile
    RETURN
  ENDPROC
ENDDEFINE